package com.study.goal.service.impl;

import cn.hutool.core.bean.BeanUtil;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.study.common.core.domain.PageQuery;
import com.study.common.core.page.TableDataInfo;
import com.study.common.utils.StringUtils;
import com.study.goal.domain.IntegralExchangeRecord;
import com.study.goal.domain.bo.IntegralExchangeRecordBo;
import com.study.goal.domain.vo.IntegralExchangeRecordVo;
import com.study.goal.mapper.IntegralExchangeRecordMapper;
import com.study.goal.service.IIntegralExchangeRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 积分兑换记录Service业务层处理
 *
 * @author MT
 * @date 2025-09-03
 */
@RequiredArgsConstructor
@Service
public class IntegralExchangeRecordServiceImpl implements IIntegralExchangeRecordService {

    private final IntegralExchangeRecordMapper baseMapper;

    /**
     * 查询积分兑换记录
     */
    @Override
    public IntegralExchangeRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询积分兑换记录列表
     */
    @Override
    public TableDataInfo<IntegralExchangeRecordVo> queryPageList(IntegralExchangeRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IntegralExchangeRecord> lqw = buildQueryWrapper(bo);
        Page<IntegralExchangeRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询积分兑换记录列表
     */
    @Override
    public List<IntegralExchangeRecordVo> queryList(IntegralExchangeRecordBo bo) {
        LambdaQueryWrapper<IntegralExchangeRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IntegralExchangeRecord> buildQueryWrapper(IntegralExchangeRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IntegralExchangeRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, IntegralExchangeRecord::getUserId, bo.getUserId());
        lqw.eq(bo.getItemId() != null, IntegralExchangeRecord::getItemId, bo.getItemId());
        lqw.eq(bo.getIntegralCost() != null, IntegralExchangeRecord::getIntegralCost, bo.getIntegralCost());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), IntegralExchangeRecord::getOrderNo, bo.getOrderNo());
        lqw.eq(StringUtils.isNotBlank(bo.getExchangeStatus()), IntegralExchangeRecord::getExchangeStatus, bo.getExchangeStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getDeliveryInfo()), IntegralExchangeRecord::getDeliveryInfo, bo.getDeliveryInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getUseStatus()), IntegralExchangeRecord::getUseStatus, bo.getUseStatus());
        lqw.eq(bo.getExpireTime() != null, IntegralExchangeRecord::getExpireTime, bo.getExpireTime());
        return lqw;
    }

    /**
     * 新增积分兑换记录
     */
    @Override
    public Boolean insertByBo(IntegralExchangeRecordBo bo) {
        IntegralExchangeRecord add = BeanUtil.toBean(bo, IntegralExchangeRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改积分兑换记录
     */
    @Override
    public Boolean updateByBo(IntegralExchangeRecordBo bo) {
        IntegralExchangeRecord update = BeanUtil.toBean(bo, IntegralExchangeRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IntegralExchangeRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除积分兑换记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
